﻿namespace KeppelLand.Controllers
{
    using KeppelLand.Entities;
    using Microsoft.Reporting.WebForms;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Web;


    /// <summary>
    /// Summary description for ReportController
    /// </summary>
    public class ReportController
    {
        public ReportController()
        {
            
        }

        public static byte[] GetClaimReport(string format)
        {
            string deviceInfo = null;
            // Out parameters when rendering
	        string mimeType;
	        string encoding;
	        string fileNameExtension;
	        string[] streams;
	        Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/ClaimReport2.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("Claim_Dataxx", GetClaimReportDataSet()));
            report.SetParameters(new ReportParameter("ClaimantName", "zthex"));
            byte[] bytes = report.Render(format, deviceInfo, 
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetBlanksheetReport(string format, string domainName, ClaimStatus status)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Blanksheet.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", 
                ClaimController.GetBlankClaimDataTable(DateTime.Now.ToString("yyyy-12-01"))));
            report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimsPendingSubmissionReport(string format, string domainName, ClaimStatus status)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claims_Pending_Submission.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimDataTable(domainName, status)));
            report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimsPendingApprovalReport(string format, string domainName, string filter)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claims_Pending_Approval.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimPendingApprovalDataTable(domainName, filter)));
            report.SetParameters(new ReportParameter("SupervisorName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimsSubmittedToHRReport(string format, string domainName, string filter)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claims_Submitted_To_HR.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimSubmittedToHRDataTable(filter)));
            //report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimHistoryReport(string format, string domainName)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claim_History.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimDataTable(domainName)));
            report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimHistoryReport(string format, string domainName, string where_clause_modifications)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claim_History.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimDataTable(domainName, where_clause_modifications)));
            report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetClaimHistoryReport(string format, string domainName, ClaimStatus status)
        {
            string deviceInfo = null;
            // Out parameters when rendering
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            string serverPath = HttpContext.Current.Server.MapPath("~/Reports/Claim_History.rdlc");
            LocalReport report = new LocalReport();
            using (System.IO.StreamReader r = new StreamReader(serverPath))
            {
                report.LoadReportDefinition(r);
            }

            report.DataSources.Add(new ReportDataSource("myClaim_DataTable", ClaimController.GetClaimDataTable(domainName, status)));
            report.SetParameters(new ReportParameter("ClaimantName", KeppelLand.Helpers.TranslationHelper.GetNameLegalByDomainName(domainName)));
            byte[] bytes = report.Render(format, deviceInfo,
                out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return bytes;
        }

        public static byte[] GetOriginalOvertimeForm()
        {
            string filePath = HttpContext.Current.Server.MapPath("~/Content/templates/overtime_form.xls");
            byte[] bytes = File.ReadAllBytes(filePath);
            return bytes;
        }

        private static DataTable GetClaimReportDataSet()
        {
            System.Data.DataSet ds = new DataSet();
            System.Data.DataTable dt = null;

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EOCS"].ConnectionString))
            {

                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM Claim;";

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                da.Fill(ds, "aa");
                dt = ds.Tables["aa"];
            }

            return dt;
        }

        private static DataTable GetClaimantDataSet()
        {
            System.Data.DataSet ds = new DataSet();
            System.Data.DataTable dt = null;

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EOCS"].ConnectionString))
            {

                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM B2E WHERE EmployeeMaster = 'S7940180C';";

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                da.Fill(ds, "OK");
                dt = ds.Tables["OK"];
            }

            return dt;
        }


    }
}